package com.yj.ccs.abs.web.management.controller;

import com.yj.ccs.abs.boot.authorization.annotation.CurrentUser;
import com.yj.ccs.abs.boot.authorization.annotation.UserRole;
import com.yj.ccs.abs.boot.pagination.Page;
import com.yj.ccs.abs.common.dto.IdDto;
import com.yj.ccs.abs.common.enums.UserRoleType;
import com.yj.ccs.abs.common.model.BaseResult;
import com.yj.ccs.abs.web.management.dto.CreateCompanyDto;
import com.yj.ccs.abs.web.management.dto.IdListDto;
import com.yj.ccs.abs.web.management.dto.KeywordDto;
import com.yj.ccs.abs.web.management.dto.UpdateCompanyDto;
import com.yj.ccs.abs.web.management.entity.Company;
import com.yj.ccs.abs.web.management.entity.CompanyDetail;
import com.yj.ccs.abs.web.management.entity.CompanyPageItem;
import com.yj.ccs.abs.web.management.entity.User;
import com.yj.ccs.abs.web.management.service.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/3/15
 * Author     : Unbelievable9
 **/
@Api(tags = "Web-Company-Controller")
@RestController
@RequestMapping("/web/company")
public class CompanyController {

    private final CompanyService companyService;

    @Autowired
    public CompanyController(CompanyService companyService) {
        this.companyService = companyService;
    }

    @ApiOperation("获取公司列表")
    @GetMapping("/list")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> list(KeywordDto keywordDto,  @CurrentUser User user) {
        Page<CompanyPageItem> companyPage = companyService.getCompanyPage(keywordDto);

        return new ResponseEntity<>(BaseResult.success("获取公司列表成功").setData(companyPage), HttpStatus.OK);
    }

    @ApiOperation("添加公司")
    @PostMapping(value = "/create")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> create(@RequestBody @Validated CreateCompanyDto createCompanyDto, @CurrentUser User user) {
        Company company = companyService.getCompanyByName(createCompanyDto.getName());

        if (company != null) {
            return new ResponseEntity<>(BaseResult.error("公司名称已存在, 无法添加公司"), HttpStatus.OK);
        } else {
            companyService.createCompany(createCompanyDto);

            return new ResponseEntity<>(BaseResult.success("添加公司成功"), HttpStatus.OK);
        }
    }

    @ApiOperation("获取公司详情")
    @GetMapping("/detail")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> detail(@Validated IdDto idDto, @CurrentUser User user) {
        Company company = companyService.getCompanyById(idDto.getId());

        if (company == null) {
            return new ResponseEntity<>(BaseResult.error("公司不存在"), HttpStatus.OK);
        } else {
            CompanyDetail companyDetail = companyService.getCompanyDetail(company);

            return new ResponseEntity<>(BaseResult.success("获取公司详情成功").setData(companyDetail), HttpStatus.OK);
        }
    }

    @ApiOperation("编辑公司信息")
    @PostMapping("/update")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> update(@RequestBody @Validated UpdateCompanyDto updateCompanyDto, @CurrentUser User user) {
        Company company = companyService.getCompanyById(updateCompanyDto.getId());

        if (company == null) {
            return new ResponseEntity<>(BaseResult.error("公司不存在"), HttpStatus.OK);
        } else {
            companyService.updateCompany(updateCompanyDto);

            return new ResponseEntity<>(BaseResult.success("编辑公司信息成功"), HttpStatus.OK);
        }
    }

    @ApiOperation("删除公司")
    @PostMapping("/delete")
    @UserRole(UserRoleType.ORIGINATOR)
    public ResponseEntity<BaseResult> delete(@RequestBody @Validated IdListDto idListDto, @CurrentUser User user) {
        companyService.deleteCompany(idListDto);

        return new ResponseEntity<>(BaseResult.success("删除公司成功"), HttpStatus.OK);
    }
}

